﻿package net.wangit.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class PropertiesUtil {

	private static final Log log = LogFactory
			.getLog(net.wangit.util.PropertiesUtil.class);

	public static Properties getPropertiesByFileName(String fileName) {
		try {
			InputStream stream = net.wangit.util.PropertiesUtil.class
					.getResourceAsStream((new StringBuilder("/")).append(
							fileName).toString());
			return getProperties(stream);
		} catch (Exception e) {
			log.error((new StringBuilder("could not open stream from : "))
					.append(fileName).toString());
			return null;
		}
	}

	public static Properties getPropertiesByPath(String path) {
		try {
			InputStream stream = new FileInputStream(path);
			return getProperties(stream);
		} catch (Exception e) {
			log.error((new StringBuilder("could not open stream from : "))
					.append(path).toString());
			return null;
		}
	}

	private static Properties getProperties(InputStream stream) {
		Properties result;
		if (stream == null)
			return null;
		result = new Properties();
		Properties properties = null;
		try {

			result.load(stream);
			properties = result;
			try {
				stream.close();
			} catch (IOException ioe) {
				log.error("could not close stream", ioe);
			}
		} catch (Exception ex) {
			log.error("could not load stream ");
		} finally {
			try {
				stream.close();
			} catch (IOException ioe) {
				log.error("could not close stream", ioe);
			}
		}
		return properties;
	}

	public static boolean getBoolean(Properties props, String property) {
		return Boolean.valueOf(props.getProperty(property)).booleanValue();
	}

	public static boolean getBoolean(Properties props, String property,
			boolean defaultValue) {
		String setting = props.getProperty(property);
		return Checker.isEmpty(setting) ? defaultValue : Boolean.valueOf(
				setting).booleanValue();
	}

	public static int getInt(Properties props, String property, int defaultValue) {
		String propValue = props.getProperty(property);
		return Checker.isEmpty(propValue) ? defaultValue : Integer
				.parseInt(propValue);
	}

	public static long getLong(Properties props, String property,
			long defaultValue) {
		String propValue = props.getProperty(property);
		return Checker.isEmpty(propValue) ? defaultValue : Long
				.parseLong(propValue);
	}

	public static String getString(Properties props, String property,
			String defaultValue) {
		String propValue = props.getProperty(property);
		return Checker.isEmpty(propValue) ? defaultValue : propValue;
	}

	public static Map<String, String> toMap(String property, String delim,
			Properties properties) {
		Map<String, String> map = new HashMap<String, String>();
		String propValue = properties.getProperty(property);
		if (propValue != null) {
			StringTokenizer tokens = new StringTokenizer(propValue, delim);
			while (tokens.hasMoreTokens()) {
				map.put(tokens.nextToken(), tokens.hasMoreElements() ? tokens
						.nextToken() : "");
			}
		}
		return map;
	}

	public static String[] toStringArray(String property, String delim,
			Properties props) {
		return toStringArray(props.getProperty(property), delim);
	}

	public static String[] toStringArray(String propValue, String delim) {
		if (propValue != null)
			return StringUtil.split(delim, propValue);
		else
			return EmptyObject.STRING_ARRAY;
	}

	private PropertiesUtil() {
	}

}
